﻿using System;
using System.IO;
using System.Text;

namespace Tools.Utilities
{
    public sealed class Logger
    {
       /* /// <summary>
        /// Repository identifier.
        /// </summary>
        private static readonly string repositiry = Guid.NewGuid().ToString("N");

        private static ILog log;

        /// <summary>
        /// Default configuration template.
        /// </summary>
        private static string configurationTemplate =
            @"<log4net>  
                <appender name='RollingLogFileAppender' type='log4net.Appender.RollingFileAppender'>
                    <file value=""{0}""/>
                    <appendToFile value='true'/>
                    <datePattern value='yyyy-MM-dd'/>
                    <rollingStyle value='Date'/>
                    <maxSizeRollBackups value='10'/>
                    <lockingModel type='log4net.Appender.FileAppender+MinimalLock' />
                    <layout type='log4net.Layout.PatternLayout'>
                        <conversionPattern value='%d %ndc - %message%newline'/>
                    </layout>                    
                </appender> 
                <root>
                    <appender-ref ref='RollingLogFileAppender'/>
                </root>
            </log4net>";

        private Logger()
        {
        }

        /// <summary>
        /// Gets log.
        /// </summary>
        private static ILog Writer
        {
            get
            {
                lock (typeof(Logger))
                {
                    if (log == null)
                    {
                        XmlConfigurator.Configure();
                        log = LogManager.Exists(typeof(Logger).FullName + ".");
                        if (log == null)
                        {
                            string path = ConfigurationManager.AppSettings["LogPath"];
                            if (string.IsNullOrEmpty(path))
                            {
                                path = "Logs/Log.txt";
                            }

                            if (!Path.IsPathRooted(path))
                            {
                                path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);
                            }

                            Configure(repositiry, path);
                            log = LogManager.GetLogger(repositiry, "root");
                        }
                    }

                    return log;
                }
            }
        }

        /// <summary>
        /// Writes info to the specified logger.
        /// </summary>        
        /// <param name="message">Message to log.</param>
        public static void Info(string message)
        {
            Writer.Info(message);
        }

        /// <summary>
        /// Writes warning to the specified logger.
        /// </summary>        
        /// <param name="message">Message to log.</param>
        public static void Warn(string message)
        {
            Writer.Warn(message);
        }

        /// <summary>
        /// Writes error to the specified logger.
        /// </summary>        
        public static void Error(string message)
        {
            Writer.Error(message);
        }

        /// <summary>
        /// Writes error to the specified logger.
        /// </summary>             
        public static void Error(string message, Exception exception)
        {
            Writer.Error(message, exception);
        }

        /// <summary>
        /// Writes fatal error to the specified logger.
        /// </summary>
        /// <param name="exception"></param>
        public static void Fatal(Exception exception)
        {
            Writer.Fatal(exception.Message, exception);
        }

        /// <summary>
        /// Writes fatal error to the specified logger.
        /// </summary>        
        public static void Fatal(string message)
        {
            Writer.Fatal(message);
        }

        /// <summary>
        /// Writes fatal error to the specified logger.
        /// </summary>             
        public static void Fatal(string message, Exception exception)
        {
            Writer.Fatal(message, exception);
        }

        /// <summary>
        /// Configures repository.
        /// </summary>
        /// <param name="repository">Repository identifier.</param>
        /// <param name="path">Log file path.</param>
        private static void Configure(string repository, string path)
        {
            string configuration = string.Format(
                configurationTemplate, path.Replace("\\", "/").Replace("&", "&amp;").Replace("\"", "&qout;"));

            Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(configuration));
            XmlConfigurator.Configure(LogManager.CreateRepository(repository), stream);
        }*/
    }
}
